//
//  PerfectInformationViewController.m
//  EMMApp
//
//  Created by Mac on 2017/8/15.
//  Copyright © 2017年 YiXun. All rights reserved.
//

#import "PerfectInformationViewController.h"
#import "PerfectInCell.h"
@interface PerfectInformationViewController ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate>

@property (nonatomic, strong) UITableView *perfectInformationTableView;

@property (nonatomic, strong) NSString *perfectString;
@end

@implementation PerfectInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"客户信息完善";
    self.view.backgroundColor = [UIColor colorFromHexCode:@"#F7F7F7"];
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setFrame:CGRectMake(0, 0, 54 ,44)];
    [backBtn addTarget:self
                action:@selector(NewbackToPreviousPage:)
      forControlEvents:UIControlEventTouchUpInside];
    [backBtn setBackgroundColor:[UIColor clearColor]];
    [backBtn setImage:[UIImage imageNamed:@"ico_back"] forState:UIControlStateNormal];
    backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    if (EMM_WIDTH <= 320) {
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
    }else if (EMM_WIDTH > 375){
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    }else{
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    [self.view addSubview:self.perfectInformationTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 返回按钮调用方法
- (void)NewbackToPreviousPage:(id) send {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 初始化UItableView
- (UITableView *)perfectInformationTableView {
    if (!_perfectInformationTableView) {
        _perfectInformationTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, EMM_HEIGHT-SafeAreaTopHeight) style:UITableViewStyleGrouped];
        _perfectInformationTableView.delegate = self;
        _perfectInformationTableView.dataSource = self;
        _perfectInformationTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _perfectInformationTableView.showsVerticalScrollIndicator = NO;
        _perfectInformationTableView.backgroundColor = [UIColor colorFromHexCode:@"#F7F7F7"];
    }
    return _perfectInformationTableView;
}

#pragma mark --- UITableViewDelegate、UITableViewDataSource 代理
//返回几个表头
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

//每一个表头下返回几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return EMM_HEIGHT-SafeAreaTopHeight;
}

//头视图的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
   
    return 0.0001;
}

//尾视图的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.0001;
}

//头部视图调用view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

//尾部视图调用View
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return nil;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"PerfectInCellId";
    PerfectInCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"PerfectInCell" owner:self options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.mySaveBtn addTarget:self action:@selector(mySaveBtnClick) forControlEvents:UIControlEventTouchUpInside];
    if (![_isTransaction isEqualToString:@"1"]) {
        cell.saveBtn.hidden = NO;
        [cell.saveBtn addTarget:self action:@selector(saveBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }else {
        cell.saveBtn.hidden = YES;
    }
    cell.perfectTextView.delegate = self;
    if ([NSString isStringEmpty2:_perfectString] == NO) {
        cell.perfectTextView.textColor = [UIColor colorFromHexCode:@"#333333"];
        cell.perfectTextView.text = _perfectString;
    }else {
        cell.perfectTextView.text = @"请填写备注";
        cell.perfectTextView.textColor = [UIColor colorFromHexCode:@"#BABABA"];
    }
    cell.titleLabel.text = [NSString stringWithFormat:@"填写信息不全不满足意向等级【%@】要求",self.khIntention];
    cell.titleSubLabel.text = [NSString stringWithFormat:@"继续保存客户为意向等级【%@】",self.khIntention];

    return cell;
}

//我要保存
- (void)mySaveBtnClick {
    if ([NSString isStringEmpty2:_perfectString] == YES) {
        [self showToast:@"选择我要保存请先填写备注"];
        return;
    }
    if (_perfectInformationVCBook) {
        _perfectInformationVCBook(_perfectString, @"0");
    }
    [self.navigationController popViewControllerAnimated:YES];
}


//按照实际意向等级保存
- (void)saveBtnClick {
//    if ([NSString isStringEmpty2:_perfectString] == YES) {
//        [self showToast:@"选择我要保存请先填写备注"];
//        return;
//    }
    if (_perfectInformationVCBook) {
        _perfectInformationVCBook(_perfectString, @"1");
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    PerfectInCell *cell = [_perfectInformationTableView cellForRowAtIndexPath:indexPath];
    if (textView == cell.perfectTextView) {
        if ([NSString isStringEmpty2:_perfectString] == YES) {
            cell.perfectTextView.text = nil;
            cell.perfectTextView.textColor = [UIColor colorFromHexCode:@"#333333"];
        }
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    PerfectInCell *cell = [_perfectInformationTableView cellForRowAtIndexPath:indexPath];
    if (textView == cell.perfectTextView) {
        _perfectString = textView.text;
        [_perfectInformationTableView reloadData];
    }
}



@end
